﻿using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR.Client;
using Microsoft.AspNet.SignalR.Client.Transports;
using Micua.iChat.Common;
using Micua.iChat.Models;
using Micua.iChat.Views;
using Windows.ApplicationModel.Core;
using Windows.Data.Json;
using Windows.Networking.Connectivity;
using Windows.Networking.Sockets;
using Windows.Storage.Streams;
using Windows.System;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Micua.iChat.ViewModels
{
    public class LoginViewModel : ViewModelBase
    {
        public LoginViewModel()
        {


            #region Commands

            LoginCommand = new Command(p =>
                {
                    var model = p as LoginViewModel;
                    if (model == null)
                    {
                        return false;
                    }
                    return !string.IsNullOrEmpty(model.Username)
                        && !string.IsNullOrEmpty(model.Password);
                }, async p =>
                {
                    var model = p as LoginViewModel;
                    if (model == null)
                    {
                        return;
                    }
                    model.Loading = true;

                    //App.Current.h

                    #region 对象无法复用

                    //// 连接到服务端
                    //var hubConnection = new HubConnection("http://localhost:3294/");
                    //// 客户端想调用服务端Hub必须创建一个Hub代理
                    //var hubProxy = hubConnection.CreateHubProxy("AccountHub");
                    //// 注册客户端方法
                    //hubProxy.On<string>("LoginFailedCallback", async msg =>
                    //{
                    //    System.Diagnostics.Debug.WriteLine(msg);
                    //    // Window.Current不是无处不在的

                    //    //await CoreApplication.MainView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                    //    //{
                    //    //    model.Loading = false;
                    //    //});
                    //});
                    //hubProxy.On<User>("LoginSuccessCallback", user =>
                    //{
                    //    System.Diagnostics.Debug.WriteLine("success" + user.Username);
                    //    //model.Loading = false;
                    //});

                    //// 启动连接 在WP中必须要指定WebSocketTransport货LongPolling两种方式传输
                    //await hubConnection.Start(new WebSocketTransport());

                    //// 调用服务端方法
                    //await hubProxy.Invoke("Login", model.Username, model.Password); 
                    #endregion



                    App.Current.AccountHubProxy.On<string>("LoginFailedCallback", async msg =>
                        {
                            System.Diagnostics.Debug.WriteLine(msg);
                            // Window.Current不是无处不在的

                            await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                            {
                                model.Loading = false;
                            });
                        });
                    App.Current.AccountHubProxy.On<User>("LoginSuccessCallback", async user =>
                        {
                            System.Diagnostics.Debug.WriteLine("success" + user.Username);
                            //model.Loading = false;
                            await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                            {
                                // 登录成功跳转到主页面
                                var rootFrame = Window.Current.Content as Frame;
                                rootFrame.Navigate(typeof(MainPage), user);
                            });
                        });

                    await App.Current.AccountHubProxy.Invoke("Login", model.Username, model.Password);
                });

            AboutCommand = new Command(p => true, async (p) =>
            {
                // 跳转到主页
                //System.Diagnostics.Debug.WriteLine("AboutCommand");
                await Launcher.LaunchUriAsync(new Uri("http://www.itcast.cn"));
            });

            ExitCommand = new Command(p => true, (p) =>
            {
                // 应用程序退出
                //System.Diagnostics.Debug.WriteLine("exit");
                App.Current.Exit(); // 之间进入Not Running状态
            });

            #endregion


            Avatar = "/Assets/avatar/10002.png";
            username = "user1";
            password = "1";
        }
        public string Avatar { get; set; }

        private string username;

        public string Username
        {
            get { return username; }
            set
            {
                SetProperty(ref username, value);
                LoginCommand.OnCanExecuteChanged();
            }
        }

        private string password;

        public string Password
        {
            get { return password; }
            set
            {
                SetProperty(ref password, value);
                LoginCommand.OnCanExecuteChanged();
            }
        }

        private bool loading;

        public bool Loading
        {
            get { return loading; }
            set { SetProperty(ref loading, value); }
        }


        public Command LoginCommand { get; set; }
        public Command AboutCommand { get; set; }
        public Command ExitCommand { get; set; }

    }
}
